package cn.itsource.org.service.impl;

import cn.itsource.basic.utils.PageList;
import cn.itsource.basic.utils.TreeUtil;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {

    //注入持久层依赖
    @Autowired
    private DepartmentMapper departmentMapper;

    //树形查询
    @Override
    public List<Department> TreeDept() {
        //调用查询方法
        List<Department> departments = departmentMapper.selectAll();
        //调用树型工具
        List<Department> list = TreeUtil.getTree(departments);
        return list;
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Department> findAll() {
        return departmentMapper.selectAll();

    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Department findOne(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    /**
     * 分页查询
     */
    @Override
    public PageList<Department> getAll(DepartmentQuery query) {
        //调用分页方法
        PageHelper.startPage(query.getPage(),query.getSize());
        //调用查询方法
        List<Department> departments = departmentMapper.findPage(query);
        //封装pageInfo对象
        PageInfo<Department> pageInfo = new PageInfo<>(departments);
        //封装自定义对象
        PageList<Department> pageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList());
        //返回分页数据
        return pageList;
    }

    /**
     * 新增和修改
     * @param department
     */
    @Override
    public void saveOrUpdate(Department department) {

        if(StringUtils.isBlank(department.getName())){
            throw new RuntimeException("部门名称必填");
        }

        if(StringUtils.isBlank(department.getSn())){
            throw new RuntimeException("部门编号必填");
        }


        //新增
        if (department.getId() == null){
            //调用新增方法
            departmentMapper.insertSelective(department);
            //调用方法
            makeParentDept(department);
            //4.修改这个对象
            departmentMapper.updateByPrimaryKey(department);

        }else {
            //调用方法
            makeParentDept(department);
            //根据主键修改,只有设置了值的字段才会被新增进去
            departmentMapper.updateByPrimaryKey(department);
        }

    }

    /**
     * ctrl + alt + m
     * @param department
     */
    private void makeParentDept(Department department) {
        Long[] parentIds = department.getParentIds();//3 8 10  // /3/8/10
        if (parentIds != null && parentIds.length > 0) {

            //1.设置parentId的值
            Long parentId = parentIds[parentIds.length - 1];
            department.setParentId(parentId);

            //2.循环  用得快捷提示:输入iter
            StringBuilder dirPath = new StringBuilder("");
            for (Long id : parentIds) {
                dirPath.append("/").append(id);
            }
            //3.加上自己的主键id值
            dirPath.append("/").append(department.getId()); //3/8/10/203
            department.setDirPath(dirPath.toString());

        } else {
            department.setParentId(null);
            department.setDirPath("/" + department.getId());
        }
    }
    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }


    /**
     * 批量删除
     */
    @Override
    public void bathDeleteById(Long[] ids) {
        departmentMapper.bathDelete(ids);
    }

    /**
     * 查询上级部门
     * @return
     */
    @Override
    public List<Department> findParentDepts() {
        return departmentMapper.findParent();
    }
}
